<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/jquery-3.5.1.min.js"></script>
	</head>
	<body>
		<form action="http://10.1.7.202:8080/login" method="post">
			用户名: <input id="username" type="text" name="username" value="zs" />
			密码: <input id="password" type="text" name="password" value="123" />
			<input id="btn" type="button" value="提交" />
		</form>

		<input id="hello" type="button" value="匿名权限: 访问hello" />
		<br />
		<input id="hello1" type="button" value="所有用户都可以访问：访问hello1" />
		<br />
		<input id="hello2" type="button" value="访问hello2" />
		<br />
		<input id="hello3" type="button" value="访问hello3" />
		<br />
		<input id="getOnlineUser" type="button" value="访问所有在线用户" />
		<br />
		<table id="table" border="1px" width="900">
			<tr align="center">
				<th>会话编号</th>
				<th>用户编号</th>
				<th>昵称</th>
				<th>登录名</th>
				<th>登录时间</th>
				<th>登录IP地址</th>
				<th>强退</th>
			</tr>
		</table>
		<br />
		<input id="logout" type="button" value="注销" />

		<script>
			function ajax(type, url, data, success) {
				$.ajax({
					type: type,
					xhrFields: {
						withCredentials: true
					},
					url: url,
					data: data,
					success: success,
					error: function(xhr, textStatus, errorThrown) {
						/*错误信息处理*/
						//alert("进入error---")
						alert("状态码：" + xhr.status)
						//当前状态,0-未初始化，1-正在载入，2-已经载入，3-数据进行交互，4-完成。
						//alert("状态:" + xhr.readyState)
						//alert("错误信息:" + xhr.statusText)
						//这里是详细的信息
						//alert("返回响应信息：" + xhr.responseText)
						var obj = JSON.parse(xhr.responseText)
						alert("错误信息：" + obj.message)
						//alert("请求状态：" + textStatus)
						//alert(errorThrown)
						//alert("请求失败")
					}
				})
			}

			function ajax_post(url, data, success) {
				ajax("post", url, data, success)
			}

			function ajax_get(url, data, success) {
				ajax("get", url, data, success)
			}

			function forcedOfflineUser(sessionId) {
				//var url = "http://10.1.7.202:8080/forcedOfflineUser?sessionId=" + sessionId;
				var url = "http://10.1.7.202:8080/forcedOfflineUser";
				var data = {
					"sessionId": sessionId
				}
				ajax_get(url, data, function(result) {
					alert(result.msg)
				})
			}

			$(function() {
				$("#btn").click(function() {
					var url = "http://10.1.7.202:8080/login";
					var data = {
						"username": $("#username").val(),
						"password": $("#password").val()
					}
					ajax_post(url, data, function(result) {
						alert(result.msg)
					})
				})

				$("#hello").click(function() {
					var url = "http://10.1.7.202:8080/hello";
					var data = {};
					ajax_get(url, data, function(result) {
						alert(result)
					})
				})

				$("#hello1").click(function() {
					var url = "http://10.1.7.202:8080/hello1";
					var data = {};
					ajax_get(url, data, function(result) {
						alert(result)
					})
				})

				$("#hello2").click(function() {
					var url = "http://10.1.7.202:8080/hello2";
					var data = {};
					ajax_get(url, data, function(result) {
						alert(result)
					})
				})

				$("#hello3").click(function() {
					var url = "http://10.1.7.202:8080/hello3";
					var data = {};
					ajax_get(url, data, function(result) {
						alert(result)
					})
				})

				$("#logout").click(function() {
					var url = "http://10.1.7.202:8080/logout";
					var data = {};
					ajax_get(url, data, function(result) {
						alert(result.msg)
					})
				})

				$("#getOnlineUser").click(function() {
					var url = "http://10.1.7.202:8080/getOnlineUser";
					var data = {};
					ajax_get(url, data, function(result) {
						//alert(result.data)
						var list = result.data
						$("#table").html(
						"<tr align='center'>" +
							"<th>会话编号</th>" +
							"<th>用户编号</th>" +
							"<th>昵称</th>" +
							"<th>登录名</th>" +
							"<th>登录时间</th>" +
							"<th>登录IP地址</th>" +
							"<th>强退</th>" +
							"</tr>")
						list.forEach((item, index, arr) => {
							$("#table").append("<tr align='center'><td>" + item.sessionId + "</td><td>" + item.userId + "</td><td>" +
								item.nickname + "</td><td>" + item.username + "</td><td>" + item.loginTime + "</td><td>" + item.loginIpAddress +
								"</td><td><a href='javascript:void(0);' onclick=\"forcedOfflineUser('" + item.sessionId + "')\">强退</a></td></tr>")
						})
					})
				})

			})
		</script>
	</body>
</html>
